package com.aplink.generic.xmlparser;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.text.TextUtils;

import com.aplink.generic.constant.MenuConstant;
import com.aplink.generic.pojo.controlspec.actionbar.ActionBarSpec;
import com.aplink.generic.pojo.controlspec.actionbar.SearchViewSpec;
import com.aplink.generic.pojo.controlspec.actionbar.SelectionSpec;
import com.aplink.generic.pojo.controlspec.menu.MenuItemSpec;
import com.aplink.generic.util.ResourcesHelper;

public class ActionBarParser {
    public static final String ACTION_BAR = "actionBar";
    public static final String BUTTON = "button";
    public static final String BUTTONS = "buttons";
    public static final String CONTENT_DATA = "contentData";
    public static final String DISPLAY_PROPERTY = "displayprop";
    public static final String FIELD = "field";
    public static final String ICON = "icon";
    public static final String ID = "id";
    public static final String IGNORE_INDEX = "ignoreIndex";
    public static final String IS_DISPLAY_HOME_AS_UP = "isDisplayHomeAsUp";
    public static final String SEARCH_FOR = "searchFor";
    public static final String SEARCH_VIEW = "searchView";
    public static final String SELECTION = "selection";
    public static final String SUB_TITLE = "subTitle";
    public static final String TITLE = "title";
    public static final String VALUE_PROPERTY = "valueprop";

    public static ActionBarParser getInstance() {
        final ActionBarParser menuParser = new ActionBarParser();
        return menuParser;
    }

    public ActionBarSpec parse(Node nodeFormat) {
        Element elementFormat = (Element) nodeFormat;
        if (elementFormat.hasChildNodes()) {
            final ActionBarSpec actionBarSpec = new ActionBarSpec();
            NodeList nodeListFormat = elementFormat
                    .getElementsByTagName(ActionBarParser.ACTION_BAR);
            if (nodeListFormat.getLength() == 0) {
                return null;
            }

            nodeFormat = nodeListFormat.item(0);
            if (nodeFormat.hasAttributes()) {
                actionBarSpec.setControlId(XMLPaserUtil.getAttrValue(
                        nodeFormat, ActionBarParser.ID));
                String value = XMLPaserUtil.getAttrValue(nodeFormat,
                        ActionBarParser.TITLE);
                if (!TextUtils.isEmpty(value)) {
                    actionBarSpec.setTitle(ResourcesHelper
                            .getStringByName(value));
                }

                value = XMLPaserUtil.getAttrValue(nodeFormat,
                        ActionBarParser.SUB_TITLE);
                if (!TextUtils.isEmpty(value)) {
                    actionBarSpec.setSubTitle(ResourcesHelper
                            .getStringByName(value));
                }

                value = XMLPaserUtil.getAttrValue(nodeFormat,
                        ActionBarParser.ICON);
                if (!TextUtils.isEmpty(value)) {
                    actionBarSpec.setIcon(value);
                }

                value = XMLPaserUtil.getAttrValue(nodeFormat,
                        ActionBarParser.IS_DISPLAY_HOME_AS_UP);
                if (!TextUtils.isEmpty(value)) {
                    if (value.equalsIgnoreCase("Y")) {
                        actionBarSpec.setDisplayHomeAsUp(true);
                    }
                }
            }

            if (nodeFormat.getNodeType() == Node.ELEMENT_NODE) {
                elementFormat = (Element) nodeFormat;
                nodeListFormat = elementFormat
                        .getElementsByTagName(ActionBarParser.SEARCH_VIEW);
                if (nodeListFormat.getLength() == 1) {
                    final SearchViewSpec searchViewSpec = parseSearchView(nodeListFormat
                            .item(0));
                    actionBarSpec.setSearchViewSpec(searchViewSpec);
                }
                nodeListFormat = elementFormat
                        .getElementsByTagName(ActionBarParser.SELECTION);
                if (nodeListFormat.getLength() == 1) {
                    final SelectionSpec selectionSpec = parseSelectionSpec(nodeListFormat
                            .item(0));
                    actionBarSpec.setSelectionSpec(selectionSpec);
                }
                nodeListFormat = elementFormat
                        .getElementsByTagName(MenuConstant.SUB_MENU);
                for (int i = 0; i < nodeListFormat.getLength(); i++) {
                    final Node nodeItem = nodeListFormat.item(i);
                    final MenuItemSpec popupMenuItem = MenuItemParser
                            .parseMenuItemSpec(nodeItem);
                    actionBarSpec.add(popupMenuItem);
                }

                nodeListFormat = elementFormat
                        .getElementsByTagName(ActionBarParser.BUTTONS);
                if (nodeListFormat.getLength() == 1) {
                    nodeListFormat = elementFormat
                            .getElementsByTagName(ActionBarParser.BUTTON);
                    for (int i = 0; i < nodeListFormat.getLength(); i++) {
                        final Node nodeItem = nodeListFormat.item(i);

                        final MenuItemSpec popupMenuItem = MenuItemParser
                                .parseMenuItemSpec(nodeItem);
                        actionBarSpec.addButton(popupMenuItem);
                    }
                }
            }
            return actionBarSpec;
        }
        return null;
    }

    private SearchViewSpec parseSearchView(final Node node) {
        final SearchViewSpec searchViewSpec = new SearchViewSpec();
        if (node.hasAttributes()) {
            String value = XMLPaserUtil.getAttrValue(node, ActionBarParser.ID);
            if (!TextUtils.isEmpty(value)) {
                searchViewSpec.setID(value);
            }

            value = XMLPaserUtil.getAttrValue(node, ActionBarParser.TITLE);
            if (!TextUtils.isEmpty(value)) {
                searchViewSpec.setTitle(value);
            }

            value = XMLPaserUtil.getAttrValue(node, ActionBarParser.ICON);
            if (!TextUtils.isEmpty(value)) {
                searchViewSpec.setIcon(value);
            }

            value = XMLPaserUtil.getAttrValue(node, ActionBarParser.SEARCH_FOR);
            if (!TextUtils.isEmpty(value)) {
                searchViewSpec.setSearchFor(value);
            }
        }

        final NodeList nodeList = ((Element) node)
                .getElementsByTagName(ActionBarParser.FIELD);
        Node field = null;
        String value = null;
        for (int i = 0; i < nodeList.getLength(); i++) {
            field = nodeList.item(i);
            value = XMLPaserUtil.getNodeValue(field);
            if (!TextUtils.isEmpty(value)) {
                searchViewSpec.addSearchField(value);
            }
        }

        return searchViewSpec;
    }

    private SelectionSpec parseSelectionSpec(final Node node) {
        final SelectionSpec selectionSpec = new SelectionSpec();
        if (node.hasAttributes()) {
            String value = XMLPaserUtil.getAttrValue(node,
                    ActionBarParser.CONTENT_DATA);
            if (!TextUtils.isEmpty(value)) {
                selectionSpec.setContentData(value);
            }

            value = XMLPaserUtil.getAttrValue(node, ActionBarParser.SEARCH_FOR);
            if (!TextUtils.isEmpty(value)) {
                selectionSpec.setSearchFor(value);
            }

            value = XMLPaserUtil.getAttrValue(node,
                    ActionBarParser.DISPLAY_PROPERTY);
            if (!TextUtils.isEmpty(value)) {
                selectionSpec.setDisplayProperty(value);
            }

            value = XMLPaserUtil.getAttrValue(node,
                    ActionBarParser.VALUE_PROPERTY);
            if (!TextUtils.isEmpty(value)) {
                selectionSpec.setValueProperty(value);
            }
            value = XMLPaserUtil.getAttrValue(node,
                    ActionBarParser.IGNORE_INDEX);
            if (!TextUtils.isEmpty(value)) {
                selectionSpec.setIgnoreIndex(Integer.parseInt(value));
            }
        }
        return selectionSpec;
    }
}
